package com.work.saturn.interesting;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.coorchice.library.SuperTextView;
import com.immo.libcommon.base.BaseFragment;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.http.NetworkRequest;
import com.work.saturn.R;
import com.work.saturn.bean.InterestListBean;
import com.immo.libcommon.constant.RouterUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author Administrator
 * @content 好玩
 * @date 2018/7/10
 */
@Route(path = RouterUtil.INTER_FRAGMENT)
public class InterestingFragment extends BaseFragment {
    @BindView(R.id.interesting_left)
    SuperTextView interestingLeft;
    @BindView(R.id.interesting_right)
    SuperTextView interestingRight;
    @BindView(R.id.interseting_list)
    RecyclerView mList;
    Unbinder unbinder;
    @BindView(R.id.interseting_empty)
    TextView intersetingEmpty;
    @BindView(R.id.main_srl)
    SwipeRefreshLayout mRefreshl;
    private boolean isPrepared = false;
    private int isRefresh;
    private List<InterestListBean.PayloadBean.BureausBean> mStringList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_interseting, container, false);
        isPrepared = true;
        lazyLoad();
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    private void initData() {
        LinearLayoutManager linearLayoutManagerTheme = new LinearLayoutManager(getActivity());
        linearLayoutManagerTheme.setOrientation(LinearLayoutManager.VERTICAL);
        mList.setLayoutManager(linearLayoutManagerTheme);
        InterestingListAdapter releaseDetailAdapter = new InterestingListAdapter();
        //设置适配器
        releaseDetailAdapter.bindToRecyclerView(mList);
        mRefreshl.setOnRefreshListener(() -> {
            isRefresh = 1;
            getListNet();
        });
    }

    private void getListNet() {
        new NetworkRequest((object, data) -> {
            if (object instanceof InterestListBean) {
                mRefreshl.setRefreshing(false);
                InterestListBean interestListBean = (InterestListBean) object;
                mStringList = interestListBean.getPayload().getBureaus();
                initData();
            }
        }).requestGet(BaseUrl.INTERESTLIST,
                getActivity(), InterestListBean.class, isRefresh == 0, false);
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        getListNet();
        isPrepared = false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.interesting_left, R.id.interesting_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.interesting_left:
                interestingLeft.setSolid(getResources().getColor(R.color.colorYellow));
                interestingLeft.setTextColor(getResources().getColor(R.color.colorWhite));
                interestingRight.setSolid(getResources().getColor(R.color.colorWhite));
                interestingRight.setTextColor(getResources().getColor(R.color.colorYellow));
                intersetingEmpty.setVisibility(View.GONE);
                mRefreshl.setVisibility(View.VISIBLE);
                break;
            case R.id.interesting_right:
                interestingLeft.setSolid(getResources().getColor(R.color.colorWhite));
                interestingLeft.setTextColor(getResources().getColor(R.color.colorYellow));
                interestingRight.setSolid(getResources().getColor(R.color.colorYellow));
                interestingRight.setTextColor(getResources().getColor(R.color.colorWhite));
                intersetingEmpty.setVisibility(View.VISIBLE);
                mRefreshl.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 资产历史列表
     */
    private class InterestingListAdapter extends BaseQuickAdapter<InterestListBean.PayloadBean.BureausBean, BaseViewHolder> implements BaseQuickAdapter.OnItemClickListener {
        InterestingListAdapter() {
            super(R.layout.item_interesting, mStringList);
            setOnItemClickListener(this);
        }

        @Override
        protected void convert(final BaseViewHolder helper, InterestListBean.PayloadBean.BureausBean item) {
            helper.setText(R.id.activity_title, item.getContent());
            helper.setText(R.id.activity_name, "公证人：" + item.getJudgePerson());
            helper.setText(R.id.activity_date, item.getEndTime() + "结束");


        }

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            ARouter.getInstance().build(RouterUtil.INTER_DETAIL)
                    .withString("name", mStringList.get(position).getName()).navigation();
        }
    }
}
